﻿/***********************************************************************
 * <copyright file="MenuAttribute.cs" company="BSD JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Thursday, April 24, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Web.Mvc;

namespace DxMvcApplication.Code.Filters
{
    /// <summary>
    /// Custom ActionFilterAttribute, used to set Selected Menu items.
    /// </summary>
    public class MenuAttribute : ActionFilterAttribute
    {
        private readonly MenuItem _selectedMenu;

        /// <summary>
        /// Constructor of MenuAttribute.
        /// Requires selected menu item enumerated value.
        /// </summary>
        /// <param name="selectedMenu">Selected menu item.</param>
        public MenuAttribute(MenuItem selectedMenu)
        {
            _selectedMenu = selectedMenu;
        }

        /// <summary>
        /// Sets selected menu in ViewData.
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewData["SelectedMenu"] = _selectedMenu.ToString().ToLower();
        }
    }
}